package A1_二叉树;

public class LC_513找树左下角的值 {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val=val;
        }
        TreeNode(int val, TreeNode left, TreeNode right){
            this.val=val;
            this.left=left;
            this.right=right;
        }
    }
    int maxDepth=Integer.MIN_VALUE;
    int result;
    public int findBottomLeftValue(TreeNode root) {
        int depth=0;
        traversal(root,depth);
        return result;
    }
    public void traversal(TreeNode node,int depth){
        if(node.left==null && node.right==null)
            if(depth>maxDepth){
                maxDepth=depth;
                result=node.val;
            }
        if(node.left!=null){
            depth++;
            traversal(node.left,depth);
            depth--;//回溯
        }
        if(node.right!=null)
            traversal(node.right,depth+1);
    }
}
